package com.newpage.module_lecture.presenter;

import android.content.Context;

import com.newpage.module_lecture.contract.LectureContract;
import com.wetao.lib_common.base.BaseResponse;
import com.wetao.lib_common.bean.response.ExamDetailInfo;
import com.wetao.lib_common.bean.response.QueryClassroomTestQuestionListInfo;
import com.wetao.lib_common.bean.response.QuestionExplainListInfo;
import com.wetao.lib_common.net.RetryWithDelay;
import com.wetao.lib_common.net.RxUtils;
import com.wetao.lib_common.net.ServerUtils;
import com.wetao.lib_common.net.callback.RequestCallback;
import com.wetao.lib_common.net.callback.RxErrorHandler;
import com.wetao.lib_common.utils.JsonUtils;

import java.util.HashMap;

import okhttp3.RequestBody;

/**
 * author: zhouyh
 * created on: 2021/6/9 3:37 下午
 * description:
 */
public class LecturePresenter extends LectureContract.Presenter {

    private Context mContext;
    public LecturePresenter(Context context) {
        mContext = context;
    }



    @Override
    public void getQuestionExplainList(HashMap<String, String> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json =(JsonUtils.getmGson()).toJson(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().getQuestionExplainList(requestBody)
                .retryWhen(new RetryWithDelay(2,1))
                .compose(RxUtils.<QuestionExplainListInfo>bindToLifecycle(getView()))
                .compose(RxUtils.<QuestionExplainListInfo>getSchedulerTransformer())
                .subscribe(new RequestCallback<QuestionExplainListInfo>(mContext, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(QuestionExplainListInfo baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().getQuestionExplainResult(baseResponse);
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        getView().getQuestionExplainFail();
                    }
                });
    }

    @Override
    public void updateUnderstand(HashMap<String, Object> map, boolean isDialog, boolean cancelable) {
        if (getView()==null)return;
        String json =(JsonUtils.getmGson()).toJson(map);
        RequestBody requestBody =  RequestBody.create(okhttp3.MediaType.parse("application/json; charset=utf-8"),json);
        ServerUtils.getCommonApi().updateUnderstand(requestBody)
                .retryWhen(new RetryWithDelay(2,1))
                .compose(RxUtils.<BaseResponse>bindToLifecycle(getView()))
                .compose(RxUtils.<BaseResponse>getSchedulerTransformer())
                .subscribe(new RequestCallback<BaseResponse>(mContext, RxErrorHandler.getInstance(),isDialog,cancelable) {
                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        super.onNext(baseResponse);
                        if(baseResponse.getCode() == 0){
                            getView().updateUnderstandSuccess(baseResponse);
                        }
                    }
                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        getView().updateUnderstandFail();
                    }
                });
    }
}
